<?php

namespace backend\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
use yii\db\Query;

/**
 * This is the model class for table "Information".
 *
 */
class Information extends ActiveRecord
{
    public $type;// 操作类型
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%information}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return[
            [['title','title','pic','info'], 'trim'],
            [['id','title','title','pic','info','status','is_recommend','mid'], 'required', 'message'=>'{attribute}不能为空'],
            [['id','type','status','is_recommend','mid','project_id'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
//            [['title','pic'], 'unique', 'message'=>'{attribute}已存在'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '资讯id',
            'title' => '标题',
            'mid'=>'分类id',
            'status'=>'资讯状态',
            'is_recommend'=>'是否推荐',
            'project_id'=>'绑定项目',
            'pic'=>'封面图',
            'info'=>'资讯详情',
            'type'=>'操作类型',
        ];
    }

    public function scenarios()
    {
        return [
            'default_create' => [
                'title',
                'mid',
                'status',
                'is_recommend',
                'pic',
                'info',
                'type',
                'project_id',
            ],
            'default_update' => [
                'id',
                'title',
                'mid',
                'status',
                'is_recommend',
                'pic',
                'info',
                'type',
                'project_id',
            ],
            'create' => [
                'title',
                'mid',
                'status',
                'is_recommend',
                'project_id',
                'user_id',
                'pic',
                'info',
                'created_at',
                'updated_at',
                'deleted_at'
            ],
            'update' => [
                'id',
                'title',
                'mid',
                'status',
                'is_recommend',
                'project_id',
                'user_id',
                'pic',
                'info',
                'created_at',
                'updated_at',
                'deleted_at'
            ],
        ];
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }


    /**
     * 获取资讯列表
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getInformationList($params){
        $condition['information.deleted_at']=null;
        if(!empty($params['title'])){
            $condition = ['and',['like','information.title',$params['title']],['information.deleted_at'=>null]];
        }
        $list = (new Query())
            ->select('information.id,information.title as name,information_class.title as class,admin.id as user_id,admin.username,information.is_recommend,information.status,FROM_UNIXTIME(information.created_at) as time')
            ->from('information')
            ->leftJoin('information_class','information_class.id=information.mid')
            ->leftJoin('admin','admin.id=information.user_id')
            ->where($condition)
            ->orderBy('information.id desc')
            ->all();
        if(count($list)){
            foreach($list as $key=>$value){
                $list[$key]['user_id'] = $value['username'].'/'.$value['user_id'];
                unset($list[$key]['username']);
                    if($value['status']>1){
                        $list[$key]['status'] ='停用';
                    }else{
                        $list[$key]['status'] ='启用';
                    }
            }
        }
        return empty($list)?array():$list;

    }

    /**
     * 删除资讯
     * @param $id
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public  function delInformation($id){
        $model =  static::findOne(['id' =>$id]);
        if(empty($model)) return false;
        $res = $model->delete();
        if($res){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 新增与编辑资讯
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function editInformation($param){
        if($param['type']>1){ //编辑
            $model = static::findOne(['id' =>$param['id']]);
            if(empty($model)) return array('error'=>'未查询到角色信息');
            $model->scenario = 'update';
        }else{
            $model = new self();
            $model->scenario = 'create';
            unset($param['id']);
        }
        $model->setAttributes($param, false);
        if (!$model->save()) {
            return array('error'=>'操作失败');
        }else{
            return array('error'=>'');
        }
    }


}
